<?php

namespace App\Http\Controllers\group;

use App\Models\GroupMember;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class MemberController extends BaseController
{
    public function preview()
    {
        $userId = auth('jwt')->id();
        $total = GroupMember::query()
            ->where([
                'leader_id' => $userId,
            ])
            ->count();

        $list = GroupMember::join('groups','group_members.user_id','groups.user_id')
            ->where('leader_id',$userId)
            ->where('groups.user_id','!=',$userId)
            ->select('group_members.user_id')
            ->get();
        $list = $list ? $list->toArray() : [];
        if($list){

            $userArr = array_column($list,"user_id");
            $total2 = GroupMember::whereIn('leader_id',$userArr)->count();
            $total += $total2;
        }
        $yesterday = GroupMember::query()
            ->where([
                'leader_id' => $userId,
            ])
            ->whereDate('created_at', Carbon::yesterday())
            ->count();

        $today = GroupMember::query()
            ->where([
                'leader_id' => $userId,
            ])
            ->whereDate('created_at', Carbon::today())
            ->count();

        return $this->success([
            'total' => $total,
            'yesterday' => $yesterday,
            'today' => $today,
        ]);
    }

    public function detail(Request $request)
    {
        $userId = auth('jwt')->id();

        $total = GroupMember::query()
            ->where([
                'leader_id' => $userId,
            ])
            ->count();

        $list = GroupMember::join('groups','group_members.user_id','groups.user_id')
            ->where('leader_id',$userId)
            ->where('groups.user_id','!=',$userId)
            ->select('group_members.user_id')
            ->get();
        $list = $list ? $list->toArray() : [];
        if($list){

            $userArr = array_column($list,"user_id");
            $total2 = GroupMember::whereIn('leader_id',$userArr)->count();
            $total += $total2;
        }
        $leaders = GroupMember::query()
            ->with('member:id,uuid,nickname')
            ->where('leader_id', $userId)
            ->whereHas('member', function ($query) {
                $query->where('is_group_leader', 1);
            })
            ->count();

        $members = GroupMember::query()
            ->with([
                'member:id,parent_id,uuid,nickname,avatar,is_group_leader,registered_at',
                'member.parent:id,parent_id,uuid,nickname,avatar,is_group_leader,registered_at',
            ])
            ->where('leader_id', $userId)
            ->get();
        $list = [];
        foreach ($members as $member) {
            $list[] = $member->member;
        }

        usort($list, function ($a, $b) {
            return $b->is_group_leader <=> $a->is_group_leader;
        });

        return $this->success([
            'total' => $total,
            'leaders' => $leaders,
            'list' => $list,
        ]);
    }
}
